package com.MedDroid.entities;

import com.MedDroid.MainApp;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Romen
 * Date: 01/12/13
 * Time: 8:38 PM
 * Description:
 */
public class Permissions
{
	public static final int PZero = 0;
	public static final int PAddUser = 1;
	public static final int PModifyUser = 2;
	public static final int PViewContact = 3;
	public static final int PModifyContact = 4;
	public static final int PViewAppointments = 5;
	public static final int PModifyAppointments = 6;
	public static final int PRequestAppointments = 7;
	public static final int PViewDoctor = 8;
	public static final int PSetDoctor = 9;
	public static final int PViewMedical = 10;
	public static final int PModifyMedical = 11;
	public static final int PGetNotifications = 12;
	public static final int PViewShots = 13;
	public static final int PModifyShots = 14;
	public static final int PViewChart = 15;
	public static final int PModifyChart = 16;
	public static final int PExportCalendar = 17;
	public static final int PCount = 18;

	public static final int USuper = 0;
	public static final int UAdmin = 1;
	public static final int UDoctor = 2;
	public static final int UNurse = 3;
	public static final int UClerical = 4;
	public static final int UPatient = 5;
	public static final int UZero = 6;

	public static final String Separator = "~~~";

	int UserType;
	boolean[] Access;
	ArrayList<Integer> UserAccess;

	public Permissions(int UserType)
	{
		this.UserType = UserType;
		Access = new boolean[PCount];
		UserAccess = new ArrayList<Integer>();
		switch (UserType)
		{
			case 0:
				setSuperUser();
				break;
			case 1:
				setAdmin();
				break;
			case 2:
				setDoctor();
				break;
			case 3:
				setNurse();
				break;
			case 4:
				setClerical();
				break;
			case 5:
				setPatient();
				break;
			case 6:
				setPZero();
				break;
			default:
				setPZero();
				break;
		}
	}

	public Permissions(String Type, String PString, String UString)
	{
		this.UserType = Integer.parseInt(Type);
		Access = new boolean[PCount];
		UserAccess = new ArrayList<Integer>();
		String[] Split = PString.split(Separator);
		String[] USplit = UString.split(Separator);
		for (int i=0; i<PCount; i++)
		{
			Access[i] = Boolean.parseBoolean(Split[i]);
		}
		for (int i=0; i<USplit.length; i++)
		{
			addUserAccess(Integer.parseInt(USplit[i]));
		}
	}

	private void setSuperUser()
	{
		Access[PZero] = true;
		Access[PAddUser] = true;
		Access[PModifyUser] = true;
		Access[PViewContact] = true;
		Access[PModifyContact] = true;
		Access[PViewAppointments] = true;
		Access[PModifyAppointments] = true;
		Access[PRequestAppointments] = true;
		Access[PViewDoctor] = true;
		Access[PSetDoctor] = true;
		Access[PViewMedical] = true;
		Access[PModifyMedical] = true;
		Access[PGetNotifications] = true;
		Access[PViewShots] = true;
		Access[PModifyShots] = true;
		Access[PViewChart] = true;
		Access[PModifyChart] = true;
		Access[PExportCalendar] = true;
	}

	private void setAdmin()
	{
		Access[PZero] = false;
		Access[PAddUser] = true;
		Access[PModifyUser] = true;
		Access[PViewContact] = true;
		Access[PModifyContact] = true;
		Access[PViewAppointments] = true;
		Access[PModifyAppointments] = true;
		Access[PRequestAppointments] = true;
		Access[PViewDoctor] = true;
		Access[PSetDoctor] = true;
		Access[PViewMedical] = true;
		Access[PModifyMedical] = true;
		Access[PGetNotifications] = true;
		Access[PViewShots] = true;
		Access[PModifyShots] = true;
		Access[PViewChart] = true;
		Access[PModifyChart] = true;
		Access[PExportCalendar] = true;
	}

	private void setDoctor()
	{
		Access[PAddUser] = false;
		Access[PModifyUser] = false;
		Access[PViewContact] = true;
		Access[PModifyContact] = false;
		Access[PViewAppointments] = true;
		Access[PModifyAppointments] = true;
		Access[PRequestAppointments] = true;
		Access[PViewDoctor] = true;
		Access[PSetDoctor] = true;
		Access[PViewMedical] = true;
		Access[PModifyMedical] = true;
		Access[PGetNotifications] = true;
		Access[PViewShots] = true;
		Access[PModifyShots] = true;
		Access[PViewChart] = true;
		Access[PModifyChart] = true;
		Access[PExportCalendar] = true;
	}

	private void setNurse()
	{
		Access[PAddUser] = false;
		Access[PModifyUser] = false;
		Access[PViewContact] = true;
		Access[PModifyContact] = false;
		Access[PViewAppointments] = true;
		Access[PModifyAppointments] = false;
		Access[PRequestAppointments] = true;
		Access[PViewDoctor] = true;
		Access[PSetDoctor] = false;
		Access[PViewMedical] = true;
		Access[PModifyMedical] = false;
		Access[PGetNotifications] = true;
		Access[PViewShots] = true;
		Access[PModifyShots] = true;
		Access[PViewChart] = true;
		Access[PModifyChart] = true;
		Access[PExportCalendar] = true;
	}

	private void setClerical()
	{
		Access[PAddUser] = false;
		Access[PModifyUser] = false;
		Access[PViewContact] = true;
		Access[PModifyContact] = true;
		Access[PViewAppointments] = true;
		Access[PModifyAppointments] = true;
		Access[PRequestAppointments] = true;
		Access[PViewDoctor] = true;
		Access[PSetDoctor] = false;
		Access[PViewMedical] = false;
		Access[PModifyMedical] = false;
		Access[PGetNotifications] = true;
		Access[PViewShots] = true;
		Access[PModifyShots] = true;
		Access[PViewChart] = false;
		Access[PModifyChart] = false;
		Access[PExportCalendar] = true;
	}

	private void setPatient()
	{
		Access[PAddUser] = false;
		Access[PModifyUser] = false;
		Access[PViewContact] = true;
		Access[PModifyContact] = true;
		Access[PViewAppointments] = true;
		Access[PModifyAppointments] = false;
		Access[PRequestAppointments] = true;
		Access[PViewDoctor] = true;
		Access[PSetDoctor] = false;
		Access[PViewMedical] = true;
		Access[PModifyMedical] = false;
		Access[PGetNotifications] = true;
		Access[PViewShots] = true;
		Access[PModifyShots] = false;
		Access[PViewChart] = true;
		Access[PModifyChart] = false;
		Access[PExportCalendar] = true;
	}

	private void setPZero()
	{
		Access[PAddUser] = false;
		Access[PModifyUser] = false;
		Access[PViewContact] = false;
		Access[PModifyContact] = false;
		Access[PViewAppointments] = false;
		Access[PModifyAppointments] = false;
		Access[PRequestAppointments] = false;
		Access[PViewDoctor] = false;
		Access[PSetDoctor] = false;
		Access[PViewMedical] = false;
		Access[PModifyMedical] = false;
		Access[PGetNotifications] = false;
		Access[PViewShots] = false;
		Access[PModifyShots] = false;
		Access[PViewChart] = false;
		Access[PModifyChart] = false;
		Access[PExportCalendar] = false;
	}

	public int getUserType()
	{
		return UserType;
	}

	public boolean getPermission(int PermissionName)
	{
		return Access[PermissionName];
	}

	public static String buildPermissionsString(Permissions P)
	{
		String PString = "";
		for (int i=0; i<P.Access.length; i++)
		{
			PString += Boolean.toString(P.getPermission(i));
			PString += Separator;
		}
		return PString;
	}

	public static String buildUserAccessString(Permissions P)
	{
		String UString = "";
		for (int i=0; i<P.UserAccess.size(); i++)
		{
			UString += Integer.toString(P.getUserAccess().get(i));
			UString += Separator;
		}
		return UString;
	}

	public ArrayList<Integer> getUserAccess()
	{
		return UserAccess;
	}

	public void addUserAccess(int UserID)
	{
		if (!UserAccess.contains(Integer.valueOf(UserID)))
		{
			UserAccess.add(UserID);
		}
	}

	public void removeUserAccess(int UserID)
	{
		while(UserAccess.remove(Integer.valueOf(UserID)));
	}
}
